# encoding:utf-8

import redis

prov_li = [11, 12, 13, 14, 15, 21, 22,23,
31, 32, 33, 34, 35, 36, 37, 41, 42, 43,
44, 45, 46, 50, 51, 52, 53, 54, 61, 62,
63, 64, 65]


class RDSUtill():
    def __init__(self):
        self.r = redis.Redis(host='10.15.3.34', port=6379, db=15)
        self.redis_name = 'WeiJianWei'

    def upload(self):
        '''
        上传到redis集合中，需要注意集合名称和数据库选择
        '''
        with open('./keys.txt', 'r', encoding='utf-8') as f:
            datas = f.readlines()
        datas = [x.strip() for x in datas]
        rows = []
        for oneline in datas:
            # name, prov = oneline.split('\u0001')
            # rows.append(name + '\u0001{}'.format(prov))
            rows.append(oneline)
        self.insert_data_redis('WeiJianWei', rows)

    def insert_data_redis(self, redis_name,  rows):
        """存任务"""
        pipe = self.r.pipeline()
        for row in rows:
            pipe.sadd(redis_name, row)
        pipe.execute()

    def get(self):
        '''获取集合中一个元素，返回字符串'''
        return self.r.spop(self.redis_name).decode('utf-8')

    def put(self, string):
        '''存入集合中，1成功存入，0数据已经存在了'''
        result = self.r.sadd(self.redis_name, string)
        return result

    def count(self):
        '''返回集合中的元素数量'''
        return self.r.scard(self.redis_name)

    def empty(self):
        '''判断集合是否为空'''
        return self.r.scard(self.redis_name) == 0


if __name__ == '__main__':
    rds = RDSUtill()
    rds.upload()
    # print(rds.get())
    # rds.put("310000__9250")
    print(rds.count())